package com.tool.tcp;

import java.awt.Graphics;

public class PartOne implements NewPart{
    /**旧人物动作加载 特效类的 act==-1技能特效  act==-2其他特效*/
    private String skin;//路径
    private String color;//颜色
    private int act;//动作
    private int lvl;//层级
    private Sprite tcp;//画步
    private NewPart   flyPart;
    private NewPart flyPartShadow;
    private NewPart   part;//下一层 层数低先画  默认1层  召唤兽饰品2层
    private NewPart   flyShadowPart;
    private Boolean isFly;
    public PartOne(String skin, int act, int lvl,String color) {
        this.skin = skin;
        this.act = act;
        this.lvl = lvl;
        this.color=color;
    }
    @Override
    public void draw(Graphics g, int x, int y, int dir, long time) {
        // TODO Auto-generated method stub
        if (this.act == 1){
            //跑步加快速度
            Double tmp = time * 1.5;
            time =  tmp.longValue();
        }
        if (flyPart!=null) { flyPart.drawFly(g, x, y, dir, time , 1F); }

        if (tcp==null) {loadTcp();}//加载素材
        synchronized(this){
            if (tcp!=null) {//绘制
                tcp.updateToTime(time,dir);
                tcp.draw(g, x, y);
            }
        }
        if (part!=null) {
            part.draw(g, x, y, dir, time);
        }
    }

    @Override
    public void draw(Graphics g, int x, int y, int dir, long time, float alpha) {
        // TODO Auto-generated method stub
        if (this.act == 1){
            //跑步加快速度
            Double tmp = time * 1.5;
            time =  tmp.longValue();
        }
        if (flyPart!=null) { flyPart.drawFly(g, x, y, dir, time,alpha); }

        if (tcp==null) {loadTcp();}//加载素材
        synchronized(this){
            if (tcp!=null) {//绘制
                tcp.updateToTime(time,dir);
                tcp.draw(g, x, y, alpha);
            }
        }
        if (part!=null) {
            part.draw(g, x, y, dir, time,alpha);
        }
    }


    @Override
    public void drawEnd(Graphics g, int x, int y, int dir, float alpha) {
        // TODO Auto-generated method stub
        if (lvl!=1) {
            if (part!=null) {
                part.drawEnd(g, x, y, dir, alpha);
            }
            return;
        }
        if (tcp==null) {loadTcp();}//加载素材
        synchronized(this){
            if (tcp!=null) {//绘制
                tcp.updateToTime(tcp.getTime()-1,dir);
                tcp.draw(g, x, y, alpha);
            }
        }
    }
    @Override
    public void drawBattle(Graphics g, int x, int y, int dir, long time,float alpha) {
        // TODO Auto-generated method stub
        if (tcp==null) {loadTcp();}//加载素材
        synchronized(this){
            if (tcp!=null) {//绘制
                tcp.updateToTime(time,SpriteFactory.changdir(dir,tcp.getAnimationCount()));
                tcp.draw(g, x, y , alpha);
            }
        }
        if (part!=null) {
            part.drawBattle(g,x,y,dir,time,alpha);
        }
    }

    @Override
    public void drawFly(Graphics g, int x, int y, int dir, long time, float alpha) {
        // TODO Auto-generated method stub
        if (tcp == null) { loadTcp(); }// 加载素材
        synchronized (this) {
            if (tcp != null) {// 绘制
                tcp.updateToTime(time, dir);
                tcp.draw(g, x, y, alpha);
            }
        }
    }

    @Override
    public void setFlyShadow(String skin, int act) {

    }

    @Override
    public void drawFlyShadow(Graphics g, int x, int y, int dir, long time, float alpha) {

    }

//    @Override
    public void draw(Graphics g, int x, int y, int dir, long time, int addy) {
        // TODO Auto-generated method stub

        if (flyPart != null) {
            flyPart.drawFly(g, x, y, dir, time, 1F);
        }

        if (flyPartShadow != null) {
            flyPartShadow.draw(g, x, y + addy + 40, dir, time, 1f);
        }
        if (tcp == null) {
            loadTcp();
        }//加载素材
        synchronized (this) {
            if (tcp != null) {//绘制
                tcp.updateToTime(time, dir);
                tcp.draw(g, x, y);
            }
        }
        if (part != null) {
            part.draw(g, x, y, dir, time);
        }
    }

    @Override
    public NewPart addPart(NewPart newPart) {
        // TODO Auto-generated method stub
        if (newPart==null) {
            return this;
        }
        if (newPart.getLvl()<lvl) {
            newPart.setPart(this);
            return newPart;
        }
        if (part==null) {
            part=newPart;
            return this;
        }else {
            part=part.addPart(newPart);
        }
        return this;
    }
    @Override
    public NewPart removePart(String rSkin) {
        // TODO Auto-generated method stub
        if (isD(rSkin)) {
            return part;
        }
        if (part!=null) {
            part=part.removePart(rSkin);
        }
        return this;
    }
    /**对比*/
    private boolean isD(String rSkin) {
        return rSkin.equals(skin);
    }
    @Override
    public boolean contains(int x, int y) {
        // TODO Auto-generated method stub
        if (lvl == 1) {
            synchronized (this) {
                if (tcp != null) {
                    return tcp.contains(x, y);
                }
            }
        } else if (part != null) {
            return part.contains(x, y);
        }
        return false;
    }
    @Override
    public void recycle() {
        // TODO Auto-generated method stub
        synchronized(this){
            tcp=null;
        }
        if (part!=null) {
            part.recycle();
        }
    }
    @Override
    public int getTime() {
        // TODO Auto-generated method stub
        if (lvl!=1&&part!=null) {
            return part.getTime();
        }
        synchronized(this){
            if (tcp!=null) {
                return tcp.getTime();
            }
        }
        loadTcp();
        return 1200;
    }
    @Override
    public void loadTcp() {
        // TODO Auto-generated method stub
        synchronized(this){
            if (act==-2) {
                tcp=SpriteFactory.Prepare(GetTcpPath.getMouseTcp(skin));
            }else if (act==-1) {
                tcp=SpriteFactory.Prepare(GetTcpPath.getSkillTcp(skin));
            }else {
                if (flyPart!=null) {
                    tcp=SpriteFactory.Prepare(GetTcpPath.getRoleTcp(skin,2,color));
                }else {
                    tcp=SpriteFactory.Prepare(GetTcpPath.getRoleTcp(skin,act,color));
                }

            }
        }
    }
    @Override
    public int getAct() {
        // TODO Auto-generated method stub
        if (lvl!=1&&part!=null) {
            return part.getAct();
        }
        return act;
    }
    @Override
    public void setAct(int Act) {
        // TODO Auto-generated method stub
        if (act>=0) {
            if (act!=Act) {
                act=Act;
                synchronized(this){
                    tcp=null;
                }
            }
        }
        if (part!=null) {
            part.setAct(Act);
        }
    }
    @Override
    public int getLvl() {
        // TODO Auto-generated method stub
        return lvl;
    }
    @Override
    public NewPart getPart() {
        // TODO Auto-generated method stub
        return part;
    }
    @Override
    public void setPart(NewPart part) {
        // TODO Auto-generated method stub
        this.part=part;
    }
    @Override
    public NewPart setPart(int lvl, String skin) {
        // TODO Auto-generated method stub
        if (this.lvl==lvl) {
            this.skin=skin;
            synchronized(this){
                this.tcp=null;
            }
            return this;
        }else if (part!=null) {
            return part.setPart(lvl, skin);
        }
        return this;
    }
    @Override
    public NewPart setPart(int lvl, long skin, HHOne[] ones) {
        // TODO Auto-generated method stub
        if (this.lvl==lvl) {
            NewPart newPart=new PartTwo(skin, ones, act, lvl,color);
            newPart.setPart(part);
            return newPart;
        }else if (part!=null) {
            return part.setPart(lvl, skin, ones);
        }
        return this;
    }
    @Override
    public int getPy() {
        if (lvl==1) {
            synchronized(this){
                if (tcp!=null) {
                    int py = tcp.getHightMax();//暂时未整理
                    py = py > 100 ? 100 : py;
                    return py;
                }
            }
        }else if (part!=null) {
            return part.getPy();
        }
        return -1;
    }
    @Override
    public NewPart clonePart() {
        // TODO Auto-generated method stub
        NewPart newPart=new PartOne(skin, act, lvl, color);
        if (part!=null) {newPart.setPart(part.clonePart());}
        return newPart;
    }
    @Override
    public int getAnimationCount() {
        // TODO Auto-generated method stub
        if (lvl==1) {
            synchronized(this){
                if (tcp!=null) {
                    return tcp.getAnimationCount();
                }
            }
        }else if (part!=null) {
            return part.getAnimationCount();
        }
        return 2;
    }

    @Override
    public void setFly(String skin, int act, Boolean isFly) {
        // TODO Auto-generated method stub
        if (lvl==1) {
            if (skin==null) {
                flyPart=null;
                flyShadowPart=null;
                this.isFly = null;
            }
            else {
                flyPart=SpriteFactory.createPart(skin, act, 1, null);
                flyShadowPart=SpriteFactory.createPart(skin, act, 1, null);
                this.isFly = isFly;
            }
            return;
        }
        if (part!=null) {
            part.setFly(skin,act, isFly);
            part.setFlyShadow(skin,act);
        }
    }
    public String getSkin() {
        return skin;
    }

    public void setSkin(String skin) {
        this.skin = skin;
    }
}
